const tinify = require("tinify");
tinify.key = "pFPc8NRlX8lQmNhfv0b91bjBC7kK8t9J";
const createDir = require('./createDir');
const path = require('path');
const fs = require('fs');

module.exports = function (input,output) {
    fs.access(output + path.basename(input), fs.constants.F_OK, (err) => {
        if(err){
            if(path.extname(input)=='.gif'){
                fs.copyFile(input, output + path.basename(input), (err) => {
                    if (err) throw err;
                    console.log('--'+output + path.basename(input)+'--')
                });

            }else{
                createDir(output + path.basename(input))
                const source = tinify.fromFile(input);
                source.toFile(output + path.basename(input),(err, data)=>{
                    if(err) throw err;
                    else {
                        console.log('--'+output + path.basename(input)+'--')
                    }
                });
            }
        }
    });

}